package com.example.demo.controller;

import com.example.demo.Util.GzipUtil;
import com.example.demo.dto.Dog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Action;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class GzipController {

    private byte[] ys;

    private String str = "ABCdef123中文~!@#$%^&*()_+{};/1111111111111111111111111AAAAAAAAAAAJDLFJDLFJDLFJLDFFFFJEIIIIIIIIIIFJJJJJJJJJJJJALLLLLLLLLLLLLLLLLLLLLL" +
            "LLppppppppppppppppppppppppppppppppppppppppp===========================------------------------------iiiiiiiiiiiiiiiiiiiiiii";

    public static List<Dog> dogs = new ArrayList<>();

    static {
        Dog dog = new Dog();
        dog.setAge(12);
        dog.setName("dog");
        dogs.add(dog);
    }

    @GetMapping("/compress")
    public void compress() throws IOException {

        System.out.println("原始的字符串长度为------->"+ str.length());

        ys = GzipUtil.compress(dogs.toString());

        System.out.println("压缩后的字符串长度为----->" + ys.length);

        System.out.println("\n压缩后的字符串为----->" + ys);
    }

    @GetMapping("/unCompress")
    public void unCompress() throws IOException {

        String result = GzipUtil.unCompress(ys);

        System.out.println("\n压缩后的字符串为----->" + result);
    }
}
